package pers.qianyu.month_202012.date_20201221;

import java.util.*;

/**
 * 225. 用队列实现栈
 * https://leetcode-cn.com/problems/implement-stack-using-queues/
 *
 * @author mizzle rain
 * @date 2020年12月21日20:33:46
 */
public class MyStack {
    private Queue<Integer> q = new LinkedList<>();
    private int top = -1;

    /**
     * Initialize your data structure here.
     */
    public MyStack() {
    }

    /**
     * Push element x onto stack.
     */
    public void push(int x) {
        q.offer(x);
        top = x;
    }

    /**
     * Removes the element on top of the stack and returns that element.
     */
    public int pop() {
        if (q.isEmpty()) {
            return -1;
        }
        int size = q.size();
        for (int i = 0; i < size - 1; i++) {
            this.push(q.poll());
        }
        return q.poll();
    }

    /**
     * Get the top element.
     */
    public int top() {
        return this.top;
    }

    /**
     * Returns whether the stack is empty.
     */
    public boolean empty() {
        return q.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */